import {Component, DoCheck, Injector, Input, OnChanges, OnInit, SimpleChanges} from '@angular/core';
import {getProperty} from '../../functions/core';
import {SimpleJsonFormatter} from '../../formatter/simple-json-formatter';
import {DynamicComponent} from '../../components/dynamic-component/dynamic-component.interface';

export interface ShowFormattedDataOptions {
  formatter: SimpleJsonFormatter[];
  data: any;
  split?: number[];
  showCopy?: boolean;
}

@Component({
  selector: 'ng-tools-show-formatted-data',
  templateUrl: './show-formatted-data.component.html',
  styleUrls: ['./show-formatted-data.component.scss'],
})
export class ShowFormattedDataComponent implements OnInit, OnChanges, DynamicComponent {
  dynamic: true;
  @Input() formatter: SimpleJsonFormatter[];
  @Input() item: any;
  @Input() split: number[];
  @Input() showCopy: boolean;

  set data(values: any) {
    this.formatter = values.formatter;
    this.item = values.data;
  }


  // noinspection JSMethodCanBeStatic
  get getProperty() {
    return getProperty;
  }

  constructor() {
  }

  ngOnInit() {
    if (!this.split) {
      this.split = [6, 6];
    }
    if (this.split.length !== 2) {
      throw Error('split`s length != 2');
    }
  }

  formatData() {
    this.item = SimpleJsonFormatter.formatSingleData(this.formatter, this.item);
  }

  ngOnChanges(changes: SimpleChanges): void {
    if (changes.data) {
      this.formatData();
    }
    if (changes.item) {
      this.formatData();
    }
  }

}
